﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SalesOrderAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.pnlSalesOrderInfo.Visible = false;
                //this.txtDateDelivery.Text = System.DateTime.Now.AddDays(20).ToString("yyyy-MM-dd");
                //this.txtDateInspect.Text = System.DateTime.Now.AddDays(21).ToString("yyyy-MM-dd");
                //this.txtDateInstall.Text = System.DateTime.Now.AddDays(22).ToString("yyyy-MM-dd");
                //this.txtDateOrder.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            }
        }
        protected void btnAddCustomer_Click(object sender, EventArgs e)
        {
            Sales_Customer ms = new Sales_Customer();
            ms.CellPhone = this.txtCellPhone.Text;
            ms.CustomerName = this.txtCustomerName.Text;
            ms.ModifiedDate = System.DateTime.Now;        //修改日期
            //清理字符串中的特殊符号
            ms.Phone = this.txtPhone.Text;

            try
            {
                //添加客户并获取返回的客户ID号

                mk.l.Sales_Customer.InsertOnSubmit(ms);
                mk.l.SubmitChanges();
                this.lblCustomerID.Text = ms.CustomerID.ToString();
                //把输入框设置为不可用
                this.txtCellPhone.Enabled = false;
                this.txtCustomerName.Enabled = false;
                this.txtPhone.Enabled = false;
                this.btnAddCustomer.Enabled = false;
                //显示添加订单信息
                this.pnlSalesOrderInfo.Visible = true;
               // this.pnlSalesOrderInfo.Enabled = true;
            }
            catch (Exception ex)
            {
                this.lblMsgAdd.Text = "添加客户信息失败！错误信息:" + ex.Message;
            }

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (this.IsValid)     //判断页面验证是否通过
            {
                Sales_SalesOrderHeader ms = new Sales_SalesOrderHeader();
                ms.Address = this.txtAddress.Text;
                ms.Designer = "AspNetDemo";
                ms.CustomerID = int.Parse(this.lblCustomerID.Text);
                ms.DateDelivery = Convert.ToDateTime(this.txtDateDelivery.Text);
                ms.DateInspect = Convert.ToDateTime(this.txtDateInspect.Text);
                ms.DateInstall = Convert.ToDateTime(this.txtDateInstall.Text);
                ms.DateOrder = Convert.ToDateTime(this.txtDateOrder.Text);
                ms.DateAllDone = Convert.ToDateTime("9999-1-1");
                ms.Designer = mk.l.Accounts_Users.FirstOrDefault().UserName;
                ms.ModifiedDate = System.DateTime.Now;
                ms.NotesInstall = this.txtNotesInstall.Text;
                ms.NotesManufacture = this.txtNotesManufacture.Text;
                ms.NotesOrder = this.txtNotesOrder.Text;
                ms.SalesOrderNumber = this.txtSalesOrderNumber.Text;
                ms.Brokerage = 0;
                ms.SubTotal = 0;
                ms.TotalDue = 0;
                ms.RealIncome = 0;
                ms.ContractAmount = 0;
                ms.PrepaidPayment = 0;
                ms.Producer = string.Empty;
                ms.ResidualPayment = 0;
                ms.RootDepartmentID = 1;
                //初始添加，状态位设置为 1
                ms.StatusAll = "1";
                ms.StatusPayment = "1";
                ms.StatusPurchaseMaterial = "1";
                ms.StatusSubmit = "1";
                try
                {
                    mk.l.Sales_SalesOrderHeader.InsertOnSubmit(ms);
                    mk.l.SubmitChanges();
                    mk.Alert("添加订单成功！将返回订单列表，" +
                        "请继续编辑用料单明细", "SalesOrderList.aspx");
                }
                catch (Exception ex)  //如果出现异常，则抛出异常
                {
                    mk.Alert("添加订单失败！请重新添加！错误信息："
                        + ex.Message, "SalesOrderAdd.aspx");
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
         //   this.pnlSalesOrderInfo.Visible = true;
            if (this.txtSearch.Text.Trim() != "")
            {
                Sales_Customer bll = new Sales_Customer();
                StringBuilder strWhere = new StringBuilder();
                string strSearch = this.txtSearch.Text;
                strWhere.AppendFormat(" or CustomerName like '%{0}%'", strSearch);
                //strWhere.AppendFormat(" or AccountNumber like '%{0}%'", strSearch);
                //strWhere.AppendFormat(" or ContactPersonName like '%{0}%'", strSearch);
                strWhere.AppendFormat(" or Phone like '%{0}%'", strSearch);
                strWhere.AppendFormat(" or CellPhone like '%{0}%'", strSearch);
                strWhere.AppendFormat(" or Address like '%{0}%'", strSearch);
          var      blls = mk.l.Sales_Customer.Where(h => h.CustomerName.ToString().ToUpper().Contains(strSearch.ToUpper())).ToList();
                //如果搜索到客户 则显示
          if (blls.Count> 0)
          {
              //设置为可见
              this.pnlCustomerSelect.Visible = true;
              this.rbtnlCustomer.DataSource = blls;
              this.rbtnlCustomer.DataTextField = "CustomerName";
              this.rbtnlCustomer.DataValueField = "CustomerID";
              this.rbtnlCustomer.DataBind();
              this.lblMsg.Visible = false;
          }
          else
          {
              this.rbtnlCustomer.Items.Clear();
              this.lblMsg.Visible = true;
              this.lblMsg.Text = "搜索不到，请添加新客户";
          }
               

            }
        }

        protected void rbtnlCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rbtnlCustomer.SelectedItem != null)
            {
                Sales_Customer bll = new Sales_Customer();
                Sales_Customer m = mk.l.Sales_Customer.Where(k => k.CustomerID == (int.Parse(this.rbtnlCustomer.SelectedValue))).FirstOrDefault();
                if (m != null)
                {
                    //显示客户信息
                    this.pnlCustomerInfo.Visible = true;
                    this.lblCustomerName.Text = m.CustomerName;
                    //this.lblAddress.Text = m.Address;
                    this.lblCellPhone.Text = m.CellPhone;
                    this.lblPhone.Text = m.Phone;
                }
            }
        }

        protected void btnConfirmCustomer_Click(object sender, EventArgs e)
        {

            this.lblCustomerID.Text = this.rbtnlCustomer.SelectedValue;
            this.rbtnlCustomer.Enabled = false;
            //显示添加订单信息
            this.pnlSalesOrderInfo.Visible = true;
            //把按钮设置为不可用
            this.btnConfirmCustomer.Enabled = false;

        }
    }
}